<?php
namespace iot\controllers;


use common\models\ParkCurrentSpace;
use common\OrderProcess;
use Yii;
use yii\web\Controller;
use yii\web\Response;

include_once '../../lib/aliyun-php-sdk-core/Config.php';
use \Iot\Request\V20170420\RegistDeviceRequest;
use \Iot\Request\V20170420\QueryDeviceRequest;
use \AliyunMNS\Client;
use \AliyunMNS\Requests\SendMessageRequest;
use \AliyunMNS\Requests\CreateQueueRequest;
use \AliyunMNS\Exception\MnsException;

/**
 * Site controller
 */
class DevController extends Controller
{
    private $ProductKey = "jmibOFirUOO";
    private $accessKeyId = "LTAIy8dX7j1omWO1";
    private $accessSecret = "E5LyOmoFLI9iTv6t3KwJgFDQyrlxAQ";
    private $endpoint ="https://1750603237294264.mns.cn-shanghai.aliyuncs.com/";

    public function actionDeviceRegist($name = null)
    {
        if(!$name)
        {
            $name =  str_replace("-","",\thamtech\uuid\helpers\UuidHelper::uuid());
        }

        $iClientProfile = \DefaultProfile::getProfile("cn-shanghai", $this->accessKeyId, $this->accessSecret);
        $client = new \DefaultAcsClient($iClientProfile);
        $request = new RegistDeviceRequest();
        $request->setDeviceName($name);
        $request->setProductKey($this->ProductKey);
        $response = $client->getAcsResponse($request);
        Yii::$app->response->format = Response::FORMAT_JSON;
        return $response;
    }

    public function actionDeviceList()
    {

        $iClientProfile = \DefaultProfile::getProfile("cn-shanghai", $this->accessKeyId, $this->accessSecret);
        $client = new \DefaultAcsClient($iClientProfile);

        $request = new QueryDeviceRequest();
        $request->setProductKey($this->ProductKey);
        $request->setCurrentPage(1);
        $request->setPageSize(10);
        $response = $client->getAcsResponse($request);

        Yii::$app->response->format = Response::FORMAT_JSON;
        return $response;
    }

    public function actionMsg()
    {
        $queueName = "aliyun-iot-".$this->ProductKey;

        $client = new Client($this->endpoint, $this->accessKeyId, $this->accessSecret);
        $queue = $client->getQueueRef($queueName);

        //  接收设备消息
        $receiptHandle = NULL;
        $result = [];
        try
        {
            // when receiving messages, it's always a good practice to set the waitSeconds to be 30.
            // it means to send one http-long-polling request which lasts 30 seconds at most.
            $res = $queue->receiveMessage(2);
            $body = $res->getMessageBody();
            $jd=json_decode($body);

            $result["消息类型"] =$jd->messagetype;
            $result["接收到的主题名称"] =$jd->topic;
            $result["内容"] =base64_decode($jd->payload)."";

            $receiptHandle = $res->getReceiptHandle();
            $queue->deleteMessage($receiptHandle);
        }
        catch (MnsException $e)
        {
            $result["未能获取异常"] =  $e;
        }

        Yii::$app->response->format = Response::FORMAT_JSON;
        return $result;
    }



}
